package com.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.pojo.R;
import com.demo.pojo.User;
import com.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController   // RestFul
@RequestMapping("/users")
@CrossOrigin
public class UserController {
    @Autowired
    private IUserService userService;
    @GetMapping
    public R getAll(){
        return new R(true,userService.list());
    }

    @GetMapping("/{id}")  // /users/1
    public R getById(@PathVariable("id") Integer id){
        return new R(true,userService.getById(id));
    }

    // { "name":"小陈","monery":500 }
    @PostMapping
    public R save(@RequestBody User user){
        return new R(userService.save(user));
    }

    // 分页
    @GetMapping("/getPage/{current}/{size}")
    public R getPage(@PathVariable("current")Integer current,@PathVariable("size")Integer size){
        IPage<User> page = new Page<>(current,size);
        return new R(true,userService.page(page));
    }
}

